import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import vueSetupExtend from 'vite-plugin-vue-setup-extend'

export default defineConfig(({ command, mode }) => {
  /**
    在 vite.config.ts 中使用环境变量。
  */
  const env = loadEnv(mode, process.cwd())
  return {
    plugins: [vue(), vueSetupExtend()],
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/assets/styles/variables.scss" as *;`,
        },
      },
    },
    server: {
      //根据环境变量配置的 port，来动态设置本地服务器启动的 port。
      port: env.VITE_PORT as any,
      open: env.VITE_OPEN as string,
      proxy: {
        '/ucenter': {
          target: 'http://localhost:8070',
          changeOrigin: true,
        },
        '/storeservice': {
          target: 'http://localhost:8070',
          changeOrigin: true,
        },
        '/deviceservice': {
          // target: "http://m.tjwl77.com:8070",
          target: 'http://localhost:8070',
          changeOrigin: true,
        },
        '/recipeservice': {
          // target: "http://m.tjwl77.com:8070",
          target: 'http://localhost:8070',
          changeOrigin: true,
        },
        '/device': {
          // target: "http://m.tjwl77.com:8070",
          target: 'http://localhost:8070',
          changeOrigin: true,
        },
        '/deviceconfig': {
          // target: "http://m.tjwl77.com:8070",
          target: 'http://localhost:8070',
          changeOrigin: true,
        },
        '/servicesupport': {
          // target: "http://m.tjwl77.com:8070",
          target: 'http://localhost:8070',
          changeOrigin: true,
        },
        '/settingservice': {
          target: 'http://localhost:8070',
          changeOrigin: true,
        },
      },
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
  }
})
